package com.example.fourtestapp.ui;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.fourtestapp.R;
import com.example.fourtestapp.entity.BusInfo;
import com.example.fourtestapp.utils.HttpUtils;
import com.example.fourtestapp.utils.ParamUtlis;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class BusFragment extends Fragment {

    private ExpandableListView expanded;
    private BusInfo info;
    private mBusAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_bus, container, false);
        initView(view);
        initData();
        return view;
    }

    private void initView(View view) {
        expanded = view.findViewById(R.id.expanded);

        expanded.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int i) {
                for (int j = 0; j < info.getROWS_DETAIL().size() ; j++){
                    if (i != j){
                        expanded.collapseGroup(j);
                    }
                }
            }
        });

    }

    private void initData() {
        String json = new ParamUtlis()
                .setParam("UserName", "user1")
                .setParam("Line", "0")
                .build();
        HttpUtils.Post("/GetBusInfo", json, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String data = response.body().string();
                info = new Gson().fromJson(data, BusInfo.class);
                if (getActivity()==null){
                    return;
                }
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        if (adapter == null){
                            adapter = new mBusAdapter();
                            expanded.setAdapter(adapter);
                        }else {
                            adapter.notifyDataSetChanged();
                        }
                    }
                });

            }
        });
    }

    class mBusAdapter extends BaseExpandableListAdapter {

        private TextView expandedName;
        private TextView expandedLine;
        private TextView expandedMileage;
        private TextView expandedPrice;
        private TextView expandedStartTime;
        private TextView expandedEndTime;
        private TextView expandedLineSites;

        @Override
        public int getGroupCount() {
            return info.getROWS_DETAIL().size();
        }

        @Override
        public int getChildrenCount(int i) {
            return info.getROWS_DETAIL().get(i).getSites().size();
        }

        @Override
        public Object getGroup(int i) {
            return info.getROWS_DETAIL().get(i);
        }

        @Override
        public Object getChild(int i, int i1) {
            return info.getROWS_DETAIL().get(i).getSites().get(i1);
        }

        @Override
        public long getGroupId(int i) {
            return i;
        }

        @Override
        public long getChildId(int i, int i1) {
            return i + i1;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
            view = LayoutInflater.from(getContext()).inflate(R.layout.expand_item, viewGroup,
                    false);
            initView(view);
            expandedName.setText(info.getROWS_DETAIL().get(i).getName());
            expandedLine.setText(info.getROWS_DETAIL().get(i).getTime().get(0).getSite() + "—"
                    + info.getROWS_DETAIL().get(i).getTime().get(1).getSite());
            expandedMileage.setText(info.getROWS_DETAIL().get(i).getMileage() + "");
            expandedPrice.setText(info.getROWS_DETAIL().get(i).getTicket() + "");
            expandedStartTime.setText(info.getROWS_DETAIL().get(i).getTime().get(0).getStarttime());
            expandedEndTime.setText(info.getROWS_DETAIL().get(i).getTime().get(0).getEndtime());
            return view;
        }

        @Override
        public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
            view = LayoutInflater.from(getContext()).inflate(R.layout.expand_item_list, viewGroup
                    , false);
            expandedLineSites = view.findViewById(R.id.expanded_line_sites);
            expandedLineSites.setText(info.getROWS_DETAIL().get(i).getSites().get(i1)+"");
            return view;
        }

        @Override
        public boolean isChildSelectable(int i, int i1) {
            return false;
        }

        private void initView(View view) {
            expandedName = view.findViewById(R.id.expanded_name);
            expandedLine = view.findViewById(R.id.expanded_line);
            expandedMileage = view.findViewById(R.id.expanded_mileage);
            expandedPrice = view.findViewById(R.id.expanded_price);
            expandedStartTime = view.findViewById(R.id.expanded_startTime);
            expandedEndTime = view.findViewById(R.id.expanded_endTime);
        }
    }
}
